// @charset "utf-8";
/**
 * 强弱评级view
 */
define(function (require, exports, module) {
	var qrpjModel = require('./qrpjmodel');
	var qrpjChart = require('./qrpjchart');
	var qrpjReadChart = require('./qrpjreadchart');
	var qrpjView = {
		param : {
			period : 'min',
			begin : 0,
			end : 0
		},
		init : function () {
			var self = this;
			var tradingDay = commonMethod.getTradingDay();
			if (tradingDay == 0) {
				var getTradeDayTimer = setTimeout(function () {
					self.init();
				}, 200);
				return;
			} else {
				clearTimeout(getTradeDayTimer);
			}
			self.param.period = 'min';
			self.param.begin = tradingDay;
			self.requestData();
			self.renderRisingProb(tradingDay);
			interactions.datePicker(tradingDay, 'qrpj', function (date) {
				self.param.period = 'min';
				self.param.begin = date;
				self.requestData();
				self.renderRisingProb(date);
			}, function (startDate, endDate) {
				self.param.period = 'day';
				self.param.begin = startDate;
				self.param.end = endDate;
				self.requestData();
			});
			self.hoverChart();
		},
		autoRefresh : function (isIn) {
			var self = this;
			var isToday = self.param.begin == commonMethod.serverTime();
			// var isTrade = commonMethod.thsQuote.isTradeTime('1A0001');
			var isTrade = commonMethod.judgeIntime(['09:30', '11:40']) || commonMethod.judgeIntime(['13:00', '15:10']);
			if (($('.tab-active').index() == 0 && isToday && isTrade && self.param.period == 'min') || isIn) {
				self.requestData();
				self.renderRisingProb(self.param.begin);
			}
		},
		requestData : function () {
			var self = this;
			qrpjModel.request(self.param, function (result) {
				// alert(JSON.stringify(result));
				self.analyticalData(result);
			});
		},
		ztData : [],
		dtData : [],
		fyzztData : [],
		newZtData : '--',
		newDtData : '--',
		newFyzztData : '--',
		analyticalData : function (result) {
			var self = this;
			if (!$.isEmptyObject(result)) {
				var ztData = [];
				var dtData = [];
				var fyzztData = [];
				var curtime = commonMethod.serverTime();
				if (self.param.period === 'min') {
					$.each(result.data, function (i, val) {
						ztData.push(val.zt);
						dtData.push(val.dt);
						fyzztData.push(val.fyzzt);
					});
					self.newZtData = ztData[ztData.length - 1];
					self.newDtData = dtData[dtData.length - 1];
					self.newFyzztData = fyzztData[fyzztData.length - 1];

					self.renderReadText(self.newZtData);
				} else {
					$.each(result, function(j, value) {
						ztData.push(value.zt);
						dtData.push(value.dt);
						fyzztData.push(value.fyzzt);
					});
					self.newZtData = ztData[ztData.length - 1];
					self.newDtData = dtData[dtData.length - 1];
					self.newFyzztData = fyzztData[fyzztData.length - 1];
				}
				// if (self.param.begin == curtime && commonMethod.judgeIntime(['08:50', '09:45'])) {
				// 	$('#view_a_readingbtn').addClass('cannot-read');
				// } else {
				$('#view_a_readingbtn').removeClass('cannot-read');
				// }
			} else {
				self.newZtData = '--';
				self.newDtData = '--';
				self.newFyzztData = '--';
				$('#view_a_readingbtn').addClass('cannot-read');
				self.renderReadText('--');
			}
			qrpjChart.render(result, self.param);
		},
		renderReadText : function (newZtData) {
			var self = this;
			var ratingText = '--';
			if (newZtData !== '--') {
				var curtime = commonMethod.serverTime();
				var grade1, grade2, grade3;
				if (curtime == self.param.begin && commonMethod.judgeIntime(['00:00', '10:00'])) {
					grade1 = 40;
					grade2 = 25;
					grade3 = 15;
				} else if (curtime == self.param.begin && commonMethod.judgeIntime(['10:00', '13:00'])) {
					grade1 = 60;
					grade2 = 40;
					grade3 = 25;
				} else {
					grade1 = 80;
					grade2 = 50;
					grade3 = 30;
				}
				if (newZtData > grade1) {
					ratingText = '强';
				} else if (newZtData > grade2 && newZtData <= grade1) {
					ratingText = '较强';
				} else if (newZtData > grade3 && newZtData <= grade2) {
					ratingText = '中性';
				} else if (newZtData <= grade3) {
					ratingText = '弱';
				}
				qrpjReadChart.renderChart(parseInt(newZtData));
			}
			$('#view_strong_qrpjread').text(ratingText);
		},
		hoverChart : function () {
			var self = this;
			$('#view_div_qrpjchart').unbind('mouseleave').mouseleave(function() {
				$('.qrpj-legend:eq(0)').text(self.newFyzztData);
				$('.qrpj-legend:eq(1)').text(self.newZtData);
				$('.qrpj-legend:eq(2)').text(self.newDtData);
			});
		},
		renderRisingProb : function (startDate) {
			var serverTime = commonMethod.serverTime();
			var type = 'qrpjhc';
			if (serverTime == startDate && commonMethod.judgeIntime(['00:01', '09:45'])) {
				interactions.renderRisingProb(type, 'NUL');
			} else {
				interactions.requestZxData(type, startDate);
			}
		}
	};
	module.exports = qrpjView;
});